//
//  KKWelcomeController.swift
//  KKWeibo
//
//  Created by kassia on 16/7/12.
//  Copyright © 2016年 kassia. All rights reserved.
//

import UIKit
import SDWebImage

class KKWelcomeController: UIViewController {

    //懒加载控件
    private lazy var bgImageView: UIImageView = {
    
        let imageView = UIImageView(image: UIImage(named: "ad_background"))
    
        return imageView
    }()
    
    //头像
    private lazy var headImageView: UIImageView = {
    
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        //判断头像地址是否为nil
        if let imageUrl = KKUserAccountModel.sharedUserAccount.userAccount? .avatar_large {

        //有头像地址
        imageView.sd_setImageWithURL(NSURL(string: imageUrl), placeholderImage: UIImage(named: "avatar_default_big"))
        
        }
        imageView.layer.cornerRadius = 42.5
        imageView.layer.masksToBounds = true

        return imageView
    }()
    
    //消息
    private lazy var messageLable: UILabel = {
    
        let lable = UILabel()//新建lable
        
        lable.font = UIFont.systemFontOfSize(15)//字体大小
        
        lable.textColor = UIColor.grayColor()//灰色
        
        if let name = KKUserAccountModel.sharedUserAccount.userAccount?.name {
        lable.text = "欢迎回来\(name)"//内容
        
        } else {
        
            lable.text = "欢迎回来"
        }
    
        //设置透明度
        lable.alpha = 0
        
        return lable
    }()
    
    //添加背景图
    override func loadView() {
        view = bgImageView
    }
    override func viewDidLoad() {
        super.viewDidLoad()

        setUpUI()
    }
    
    //执行动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        startAnimation()
    }

    //添加视图约束
    private func setUpUI() {
    
        view.addSubview(headImageView)
        view.addSubview(messageLable)
        
        headImageView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(view).offset(200)
            make.centerX.equalTo(view)
            make.size.equalTo(CGSizeMake(85, 85))
        }
       messageLable.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(headImageView.snp_bottom).offset(10)
            make.centerX.equalTo(headImageView)
        }
    
    }
    
    //执行动画
    private func startAnimation() {
    
        self.headImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(self.view).offset(100)
            
        }
        //usingSpringWithDamping 阻尼, 取值范围是0-1,阻尼越大弹簧效果越小
        //initialSpringVelocity 初始速度
        //枚举不想传入直接输入[]
        UIView.animateWithDuration(1.0, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
            
            //执行约束电话
            self.view.layoutIfNeeded()
            
            }) { (_) -> Void in
                
              UIView.animateWithDuration(1, animations: { () -> Void in
           
                //设置消息的透明度
                self.messageLable.alpha = 1
                
                }, completion: { (_) -> Void in
                    //进入首页
                    
                    NSNotificationCenter.defaultCenter().postNotificationName("SwitchRootVCNotification", object: nil)
                 
              })
        }
    
    }

}
